package com.lcb.redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/****
 * @Author:刘长柏
 * @Date: 2021-01-20 16:30
 * @Description:redis集群测试
 *****/
public class JedisClusterTest {
    public static void main(String[] args) throws IOException {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);

        Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
        jedisClusterNode.add(new HostAndPort("192.168.91.201", 8001));
        jedisClusterNode.add(new HostAndPort("192.168.91.202", 8002));
        jedisClusterNode.add(new HostAndPort("192.168.91.203", 8003));
        jedisClusterNode.add(new HostAndPort("192.168.91.201", 8004));
        jedisClusterNode.add(new HostAndPort("192.168.91.202", 8005));
        jedisClusterNode.add(new HostAndPort("192.168.91.203", 8006));

        JedisCluster jedisCluster = null;
        try {
            //connectionTimeout:指的是连接一个url的连接等待时间
            //soTimeout:指的是连接上一个url,获取response的返回等待时间
            //10 那个是重试次数
            jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "lcb", jedisPoolConfig);
            System.out.println(jedisCluster.set("lcb", "6666"));
            System.out.println(jedisCluster.get("lcb"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedisCluster != null) {
                jedisCluster.close();
            }
        }


    }
}
